package xxl.signals.util 
{
	import avmplus.DescribeTypeJSON;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	/**
	 * ...
	 * @author XXL
	 */
	public class ReflectorUtil 
	{
		private static var _INSTANCE:ReflectorUtil = null;
		
		public static function GetInstance():ReflectorUtil
		{
			if (_INSTANCE == null)
			{
				_INSTANCE = new ReflectorUtil();
			}
			return _INSTANCE;
		}
		
		private var _descriptor:DescribeTypeJSON = null;
		
		public function ReflectorUtil() 
		{
			if (DescribeTypeJSON.available)
			{
				_descriptor = new DescribeTypeJSON();
			}
		}
		
		public function typeImplements(type:Class, superType:Class):Boolean
		{
			if (type == superType)
			{
				return true;
			}
			if (_descriptor)
			{
				return typeImplementsJSONMode(type, superType);
			}
			const factoryNode:XMLList = describeType(type).factory;
			const superclassName:String = getQualifiedClassName(superType);
			return factoryNode.extendsClass.@type.contains(superclassName)
					|| factoryNode.implementsInterface.@type.contains(superclassName);
		}
		
		private function typeImplementsJSONMode(type:Class, superType:Class):Boolean 
		{
			const superClassName:String = getQualifiedClassName(superType);
			const traits:Object = _descriptor.getInstanceDescription(type).traits;
			return (traits.bases as Array).indexOf(superClassName) > -1 
				|| (traits.interfaces as Array).indexOf(superClassName) > -1;
		}
		
	}

}